<?php

/**
 * This function returns the total price of an order
 *
 * @access   public
 * @author   Svilen <svilen@bastun.net>
 *
 * @param    array   $sales     	 All sales details array (extracted from the DB and decoded)
 * @param    string  $currency     Currency of the order
 *
 * @return   float    Returns total price
 *
 * @since    20-06-2005
 * @version  1.0
 *
 */
function calculateTotalSalePrice ($sales, $currency, $walletRefill = FALSE) {
	$totalPrice = 0;
  $period = 0;
  
  if (!empty($sales) && is_array($sales)) {
  	foreach ($sales as $key => $value) {
    	$period = $value['period'];
      
    	if (!empty ($value['amount_total'])) {
      	$totalPrice += (float) $value['amount_total'];
      }
    }
  }
	
  return $totalPrice;
}

/**
 * This function extracts the domain status and name from an order array 
 *
 * @access   public
 * @author   Svilen <svilen@bastun.net>
 *
 * @param    array   $sales     	 All sales details array (extracted from the DB and decoded)
 *
 * @return   array   Returns array containig domain status and domain name
 *
 * @since    22-06-2005
 * @version  1.0
 *
 */
function getDomainDetails($sales) {
	$domain = array();
  $status = 'undefined';
	
  if (!empty($sales) && is_array($sales)) {
  	foreach ($sales as $key => $value) {
    	$period = $value['period'];
      
      if (in_array($value['product_type'], array('domain', 'registerdomain', 'res_regdomain'))) {
      	switch ($value['domain_flags']) {
        	case 0:
          	$status = 'registration';
            break;
          case 1:
          	$status = 'renewal';
          	break;
          case 2:
          	$status = 'transfer';
            break;
          case 3:
          	$status = 'change';
            break;
          case 4:
          	$status = 'approved';
            break;
          case 5:
          	$status = 'rejected';
            break;
          case 6:
          	$status = 'suspended';
            break;
          case 7:
          	$status = 'transferring';
            break;
          case 8:
          	$status = 'transfer_fail';
            break;
          case 9:
          	$status = 'expired';
            break;
        }
        
        $domain = array ('status' => $status, 'name' => $value['domain_sld'].'.'.$value['domain_tld'], 'expiration_date' => $value['purchase_expire_date']);
        
        break;				
      }
    }
  }

  return $domain;
}

/**
 * This function extracts the hosting name from an order array 
 *
 * @access   public
 * @author   Svilen <svilen@bastun.net>
 *
 * @param    array   $sales     	 All sales details array (extracted from the DB and decoded)
 *
 * @return   array   Returns array containig hosting name and status
 *
 * @since    23-06-2005
 * @version  1.0
 *
 */
function getHostingDetails($sales) {
	$hosting = array();
	 
  if (!empty($sales) && is_array($sales)) {
  	foreach ($sales as $key => $value) {
    	$period = $value['period'];
      if (in_array($value['product_type'], array('plan'))) {
       	$hosting = array ('status' => $value['state'], 'name' => $value['sale_name'], 'expiration_date' => $value['purchase_expire_date']);
        break;
      }
    }
  }

  return $hosting;
}

/**
 * This function returns currect order type 
 *
 * @access   public
 * @author   Svilen <svilen@bastun.net>
 *
 * @param    array   $sales     	 All sales details array (extracted from the DB and decoded)
 *
 * @return   string  Returns currect order type
 *
 * @since    23-06-2005
 * @version  1.0
 *
 */
function checkOrderType($sales) {
	$type = 'unknown';
	
  if (!empty($sales)) {
    // Get domain details
  	$domain = getDomainDetails($sales);
    // Get hosting details
    $hosting = getHostingDetails($sales);
  }
  
  switch ($sales[0]['sale_type']) {
  	case 'order':
    	if ($sales[0]['product_type'] == 'res_regdomain') {
      	// Register reseller domain
        $type = 'reseller_register_domain';
      }
      else {
      	// New order
      	$type = 'new_order';
      }
    	break;
		case 'renewal':
    	if (!empty($domain['name']) && !empty($hosting['name'])) {
      	$type = 'renew_domain_hosting';
      }
      elseif (!empty($domain['name'])) {
      	$type = 'renew_domain';
      }
      elseif (!empty($hosting['name'])) {
      	$type = 'renew_hosting';
      }
      else {
      	$type = 'renew_unknown';
      }
    	break;
    case 'reversal';
    	$type = 'refund_order';
    	break;
		case 'upgrade':
    	// Service upgrades
      // Domain registration through the CP
      if (!empty($domain['name']) && $domain['status'] == 'registration') {
      	$type = 'upgrade_domain_registration';
      }
      elseif (!empty($domain['name']) && $domain['status'] == 'transfer') {
      	$type = 'upgrade_domain_transfer';
      }
      else {
      	$type = 'upgrade_service';
      }
    	break;
    case 'wallet_refill':
    	$type = 'wallet_refill';
    	break;      
    case 'change plan':
    	$type = 'plan_upgrade';
    	break; 
  }

  return $type;
}

?>